using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DirkRettschlag.Utils
{
    public static class Invoking
    {

        delegate ListViewItem AddItemCallback(ListView co, ListViewItem item);
        public static ListViewItem AddItem(ListView co, ListViewItem item)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (co.InvokeRequired)
            {
                AddItemCallback d = new AddItemCallback(AddItem);
                return (ListViewItem)co.Invoke(d, new object[] { co, item });
            }
            else
            {
                return co.Items.Add(item);
            }
        }

        delegate void RemoveItemCallback(ListView co, int index);
        public static void RemoveItem(ListView co, int index)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (co.InvokeRequired)
            {
                RemoveItemCallback d = new RemoveItemCallback(RemoveItem);
                co.Invoke(d, new object[] { co, index });
            }
            else
            {
                co.Items.Remove(co.Items[index]);
            }
        }

        delegate bool GetCheckedCallback(CheckBox co);
        public static bool GetChecked(CheckBox co)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (co.InvokeRequired)
            {
                GetCheckedCallback d = new GetCheckedCallback(GetChecked);
                return (bool)co.Invoke(d, new object[] { co });
            }
            else
            {
                return co.Checked;                
            }
        }

        delegate bool GetRadioButtonCheckedCallback(RadioButton co);
        public static bool GetRadioButtonChecked(RadioButton co)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (co.InvokeRequired)
            {
                GetRadioButtonCheckedCallback d = new GetRadioButtonCheckedCallback(GetRadioButtonChecked);
                return (bool)co.Invoke(d, new object[] { co });
            }
            else
            {
                return co.Checked;
            }
        }

        delegate string GetTextCallback(Control co);
        public static string GetText(Control co)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (co.InvokeRequired)
            {
                GetTextCallback d = new GetTextCallback(GetText);
                return (string)co.Invoke(d, new object[] { co });
            }
            else
            {
                return co.Text;
            }

        }

        delegate decimal GetValueCallback(NumericUpDown co);
        public static decimal GetValue(NumericUpDown co)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (co.InvokeRequired)
            {
                GetValueCallback d = new GetValueCallback(GetValue);
                return (decimal)co.Invoke(d, new object[] { co });
            }
            else
            {
                return co.Value;
            }

        }
        

    }
}
